package com.xbao.service.impl;

import com.github.pagehelper.PageHelper;
import com.xbao.core.util.MyBeanUtil;
import com.xbao.dao.PeriodRuleItemMapper;
import com.xbao.dao.PeriodRuleMapper;
import com.xbao.model.*;
import com.xbao.model.req.PeriodRuleReq;
import com.xbao.model.vo.PeriodRuleVo;
import com.xbao.service.ClsService;
import com.xbao.service.PeriodRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 */
@Service
public class PeriodRuleServiceImpl implements PeriodRuleService {
    @Autowired
    private PeriodRuleMapper ruleMapper;
    @Autowired
    private PeriodRuleItemMapper ruleItemMapper;

    @Resource
    private ClsService clsService;

    @Override
    public void addPeriodRule(PeriodRuleReq periodRule) {
        ruleMapper.insert(periodRule);
        if(periodRule.getRuleItems()!=null&&periodRule.getRuleItems().size()>0) {
           for(PeriodRuleItem item:periodRule.getRuleItems()){
               ruleItemMapper.insert(item);
           }
        }


    }

    @Override
    public void deletePeriodRule(Long rid) {
        ruleMapper.deleteByPrimaryKey(rid);
        deletePeriodRuleItemByRuleId(rid);


    }

    @Override
    public void modifyPeriodRule(PeriodRuleReq periodRule) {
        ruleMapper.updateByPrimaryKeySelective(periodRule);
        if(periodRule.getRuleItems()!=null&&periodRule.getRuleItems().size()>0){
            deletePeriodRuleItemByRuleId(periodRule.getId());
            for(PeriodRuleItem item:periodRule.getRuleItems()){
                ruleItemMapper.insert(item);
            }
        }

    }

    @Override
    public List<PeriodRuleVo> queryPeriods(PeriodRuleReq periodRuleReq) {
        PeriodRuleExample example = new PeriodRuleExample();
        PeriodRuleExample.Criteria criteria = example.createCriteria();
        if (periodRuleReq.getPager() != null) {
            PageHelper.startPage(periodRuleReq.getPager().getPageNo(), periodRuleReq.getPager().getPageSize());
            example.setOrderByClause("id desc");
        }
        if(periodRuleReq.getOrgid()!=null){
                criteria.andOrgidEqualTo(periodRuleReq.getOrgid());
        }
        if (periodRuleReq.getCid() != null) {
            criteria.andCidEqualTo(periodRuleReq.getCid());
        }
        List<PeriodRule> rules = ruleMapper.selectByExample(example);
        List<PeriodRuleVo> rulevos = MyBeanUtil.copyList(rules, PeriodRuleVo.class);
        return rulevos;
    }

    @Override
    public PeriodRuleVo detailPeriodRule(Long id) {
        PeriodRule rule = ruleMapper.selectByPrimaryKey(id);
        PeriodRuleVo vo = MyBeanUtil.copyProperties(rule, PeriodRuleVo.class);
        return vo;

    }

    @Override
    public void addPeriodRuleItem(PeriodRuleItem item) {
        ruleItemMapper.insert(item);
    }

    @Override
    public void deletePeriodRuleItemByRuleId(Long rid) {
        PeriodRuleItemExample example=new PeriodRuleItemExample();
        PeriodRuleItemExample.Criteria criteria = example.createCriteria();
        criteria.andRuleidEqualTo(rid);
       ruleItemMapper.deleteByExample(example);

    }

    @Override
    public void addPeriodRuleItems(List<PeriodRuleItem> items) {
       for(PeriodRuleItem item:items){
           ruleItemMapper.insert(item);

       }

    }

    @Override
    public List<PeriodRuleItem> getItemsByRuleId(Long rid) {
        PeriodRuleItemExample example=new PeriodRuleItemExample();
        PeriodRuleItemExample.Criteria criteria=example.createCriteria();
        criteria.andRuleidEqualTo(rid);
        List<PeriodRuleItem> items=ruleItemMapper.selectByExample(example);
        return items;
    }

    @Override
    public List<PeriodRuleItem> getItemByCid(Long cid) {
        PeriodRuleItemExample example=new PeriodRuleItemExample();
        PeriodRuleItemExample.Criteria criteria=example.createCriteria();
        criteria.andCidEqualTo(cid);
        List<PeriodRuleItem> items=ruleItemMapper.selectByExample(example);
        return items;
    }

    @Override
    public List<PeriodRuleItem> getItemByUid(Long uid) {
        List<StuClsRel> rels=clsService.getClsRelOfStu(uid);
        List<Long> cids=new ArrayList<>();
        for(Long cid:cids){
            cids.add(cid);
        }
        PeriodRuleItemExample example=new PeriodRuleItemExample();
        PeriodRuleItemExample.Criteria   criteria=example.createCriteria();
        criteria.andCidIn(cids);
        List<PeriodRuleItem> items=ruleItemMapper.selectByExample(example);
        return items;
    }


}
